<%namespace file="dbi.inc" import="*" />
#include "dbi_${tablename}.h"
#define SQL_${_macro(tablename)}_INSERT "INSERT INTO ${fullname} SET ${_n_pks_sqlparams(columns)};"
#define SQL_${_macro(tablename)}_SELECTONE "SELECT * FROM ${fullname} WHERE ${_pks_sqlparams(columns)};"
#define SQL_${_macro(tablename)}_SELECT "SELECT * FROM ${fullname} WHERE %s LIMIT %d,%d;"
#define SQL_${_macro(tablename)}_COUNT "SELECT COUNT(*) AS _COUNT_ FROM ${fullname} WHERE %s;"
#define SQL_${_macro(tablename)}_UPDATEONE "UPDATE ${fullname} SET ${_n_pks_sqlparams(columns)} WHERE ${_pks_sqlparams(columns)};"
#define SQL_${_macro(tablename)}_UPDATE "UPDATE ${fullname} SET ${_n_pks_sqlparams(columns)} WHERE %s;"
#define SQL_${_macro(tablename)}_DELETEONE "DELETE FROM ${fullname} WHERE ${_pks_sqlparams(columns)};"
#define SQL_${_macro(tablename)}_DELETE "DELETE FROM ${fullname} WHERE %s;"

int32_t DBI${classname}::Insert(${classname} &in_${classname})
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		in_${classname}.Escape();
		mys.QueryWith(SQL_${_macro(tablename)}_INSERT
% for col_name, col in columns_nonkey.items():
%	if "string"==col.Type:
			, in_${classname}.${col.getFixName()}.c_str()
%	else:
			, in_${classname}.${col.getFixName()}
% 	endif
% endfor
		);

	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	
	return 0;
}

int32_t DBI${classname}::SelectOne(${_pks_params(columns)}, ${classname} &out_${classname})
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	int32_t iRes = 0;
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_${_macro(tablename)}_SELECTONE
% for col_name, col in columns_key.items():
%	if "string"==col.Type:
			, ${col.getFixName()}.c_str()
%	else:
			, ${col.getFixName()}
% 	endif
% endfor
		);
		mys.StoreResult();
		if(mys.FetchRow())
		{
% for col_name, col in columns.items():
			out_${classname}.${col.getFixName()} = mys.${_get_field(col)};
% endfor
			iRes = 1;
		}
		mys.Close();
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return iRes;
}

int32_t DBI${classname}::Select(const std::string &sCondition, uint32_t iStart, uint32_t iLimit, std::vector<${classname}> &outlist_${classname})
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_${_macro(tablename)}_SELECT
			, sCondition.c_str()
			, iStart
			, iLimit
		);
		mys.StoreResult();
		while(mys.FetchRow())
		{
			${classname} st;
% for col_name, col in columns.items():
			st.${col.getFixName()} = mys.${_get_field(col)};
% endfor
			outlist_${classname}.push_back(st);
		}
		mys.Close();
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return outlist_${classname}.size();
}

int32_t DBI${classname}::Count(const std::string &sCondition)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	int32_t iCount = 0;
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_${_macro(tablename)}_SELECTONE
			, sCondition.c_str()
		);
		mys.StoreResult();
		if(mys.FetchRow())
		{
			iCount = mys.GetField_Int("_COUNT_");
		}
		mys.Close();
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return iCount;
}

int32_t DBI${classname}::UpdateOne(${_pks_params(columns)}, ${classname} &in_${classname})
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		in_${classname}.Escape();
		mys.QueryWith(SQL_${_macro(tablename)}_UPDATEONE 
% for col_name, col in columns_nonkey.items():
%	if "string"==col.Type:
			, in_${classname}.${col.getFixName()}.c_str()
%	else:
			, in_${classname}.${col.getFixName()}
% 	endif
% endfor
% for col_name, col in columns_key.items():
%	if "string"==col.Type:
			, in_${classname}.${col.getFixName()}.c_str()
%	else:
			, in_${classname}.${col.getFixName()}
% 	endif
% endfor

		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	
	return 0;
}

int32_t DBI${classname}::Update(const std::string &sCondition, ${classname} &in_${classname})
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		in_${classname}.Escape();
		mys.QueryWith(SQL_${_macro(tablename)}_UPDATE
% for col_name, col in columns_nonkey.items():
%	if "string"==col.Type:
			, in_${classname}.${col.getFixName()}.c_str()
%	else:
			, in_${classname}.${col.getFixName()}
% 	endif
% endfor
			, sCondition.c_str()
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return 0;
}

int32_t DBI${classname}::DeleteOne(${_pks_params(columns)})
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_${_macro(tablename)}_DELETEONE
% for col_name, col in columns_key.items():
%	if "string"==col.Type:
			, ${col.getFixName()}.c_str()
%	else:
			, ${col.getFixName()}
% 	endif
% endfor
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return 0;
}

int32_t DBI${classname}::Delete(const std::string &sCondition)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_${_macro(tablename)}_DELETE
			, sCondition.c_str()
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return 0;
}

